package com.woniu.service.impl;

import com.woniu.entity.*;
import com.woniu.mapper.*;
import com.woniu.service.AddressService;
import com.woniu.vo.AddressVo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    private UserAddressMapper userAddressMapper;

    @Resource
    private ProvincesMapper provincesMapper;

    @Resource
    private CitiesMapper citiesMapper;

    @Resource
    private AreasMapper areasMapper;

    @Resource
    private StreetsMapper streetsMapper;

    @Resource
    private VillagesMapper villagesMapper;

    @Override
    public List<AddressVo> findAddressByUserId(Integer userId) {
        return userAddressMapper.selectAddressByUserId(userId);
    }

    @Override
    public int selectDefaultAddressByUserId(Integer userId) {
        Example example = new Example(UserAddressMapper.class);
        example.createCriteria().andEqualTo("accountId", userId)
                .andEqualTo("isDefault", 1);
        userAddressMapper.selectOneByExample(example);
        return 0;
    }

    @Override
    public int addAddressByUserId(UserAddress userAddress) throws Exception {
        userAddress.setCreateTime(new Date());
        List<UserAddress> otherUserAddresses = userAddressMapper.selectAddressByAccountId(userAddress.getAccountId());
        if (otherUserAddresses.isEmpty()) {
            userAddress.setIsDefault((byte) 1);
        }
        if (userAddress.getIsDefault() == 1) {
            // 获取所有Id用户的地址
            // 遍历其他用户的地址并将 IsDefault 值设置为 0

            for (UserAddress otherAddress : otherUserAddresses) {
                otherAddress.setIsDefault((byte) 0);
                userAddressMapper.updateByPrimaryKeySelective(otherAddress);
            }
        }
        int insert = userAddressMapper.insert(userAddress);
        return insert;
    }

    @Override
    public int selectAddressCountByUserId(Integer userId) throws Exception {
        int i = userAddressMapper.selectAddressCountByUserId(userId);
        return i;
    }

    @Override
    //@Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Provinces> findProvincesByCode() throws Exception {
        return provincesMapper.selectAll();
    }

    /**
     * 根据省市的code查询城市的地址
     */
    @Override
    //@Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Cities> findCitiesByProvinceCode(String provinceCode) throws Exception {
        Example example = new Example(Cities.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("provinceCode", provinceCode);
        return citiesMapper.selectByExample(example);
    }

    /**
     * 根据城市的code查询区县的地址
     */
    @Override
    // @Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Areas> findAreasByCityCode(String cityCode) throws Exception {
        Example example = new Example(Areas.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("cityCode", cityCode);
        return areasMapper.selectByExample(example);
    }

    /**
     * 根据区县的code查询街道的地址
     */
    @Override
    //@Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Streets> findStreetsByAreaCode(String areaCode) throws Exception {
        Example example = new Example(Streets.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("areaCode", areaCode);
        return streetsMapper.selectByExample(example);
    }

    /**
     * 根据街道的code查询居委的地址
     */
    @Override
    // @Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Villages> findVillagesByStreetCode(String streetCode) throws Exception {
        Example example = new Example(Villages.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("streetCode", streetCode);
        return villagesMapper.selectByExample(example);
    }

    @Override
    public boolean editAddress(UserAddress userAddress) {
        List<UserAddress> otherUserAddresses = userAddressMapper.selectAddressByAccountId(userAddress.getAccountId());
        if (otherUserAddresses.isEmpty()) {
            userAddress.setIsDefault((byte) 1);
        }
        if (userAddress.getIsDefault() == 1) {
            // 获取所有其他用户的地址
            // 遍历其他用户的地址并将 IsDefault 值设置为 0
            for (UserAddress otherAddress : otherUserAddresses) {
                otherAddress.setIsDefault((byte) 0);
                userAddressMapper.updateByPrimaryKeySelective(otherAddress);
            }
        }
        int i = userAddressMapper.updateByPrimaryKeySelective(userAddress);
        return i > 0;
    }

    @Override
    public boolean deleteAddressByAddressId(Integer addressId) {
        int i = userAddressMapper.deleteByPrimaryKey(addressId);
        return i > 0;
    }


}
